package com.sinothk.fc.storeMaterial.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinothk.fc.storeMaterial.entity.StoreMaterialClassifyVO;
import com.sinothk.fc.storeMaterial.entity.StoreMaterialEntity;
import com.sinothk.fc.storeMaterial.entity.StoreMaterialSearchReq;
import com.sinothk.fc.storeMaterial.service.IStoreMaterialService;
import com.sinothk.fc.storeMaterialRecord.entity.StoreMaterialRecordEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @Description: 商品物料表
 * @Author: Liangyt
 * @Date: 2022-09-23
 * @Version: V1.0
 */
@Api(tags = "商品物料表")
@RestController
@RequestMapping("/fc/storeMaterialCtrl")
@Slf4j
public class StoreMaterialController extends JeecgController<StoreMaterialEntity, IStoreMaterialService> {

    private final IStoreMaterialService goodsMaterialService;

    public StoreMaterialController(IStoreMaterialService goodsMaterialService) {
        this.goodsMaterialService = goodsMaterialService;
    }

    /**
     * 分页列表查询
     *
     * @param goodsMaterial
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "商品物料表-分页列表查询")
    @ApiOperation(value = "商品物料表-分页列表查询", notes = "商品物料表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(StoreMaterialEntity goodsMaterial,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StoreMaterialEntity> queryWrapper = QueryGenerator.initQueryWrapper(goodsMaterial, req.getParameterMap());
        Page<StoreMaterialEntity> page = new Page<StoreMaterialEntity>(pageNo, pageSize);
        IPage<StoreMaterialEntity> pageList = goodsMaterialService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param searchReq
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "分页列表-归类查询")
    @ApiOperation(value = "分页列表-归类查询", notes = "分页列表-归类查询")
    @GetMapping(value = "/listForClassify")
    public Result<?> listForClassify(StoreMaterialSearchReq searchReq,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<StoreMaterialClassifyVO> pageList = goodsMaterialService.listForClassify(new Page<>(pageNo, pageSize), searchReq);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param searchReq
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "分页列表-归类查询")
    @ApiOperation(value = "分页列表-归类查询", notes = "分页列表-归类查询")
    @GetMapping(value = "/listForBarCode")
    public Result<?> listForBarCode(StoreMaterialSearchReq searchReq,
                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        IPage<StoreMaterialEntity> pageList = goodsMaterialService.listForBarCode(new Page<>(pageNo, pageSize), searchReq);
        return Result.OK(pageList);
    }

    /**
     * 编辑
     *
     * @param goodsMaterial
     * @return
     */
    @AutoLog(value = "商品物料表-编辑")
    @ApiOperation(value = "商品物料表-编辑", notes = "商品物料表-编辑")
    @PutMapping(value = "/edit")
    public Result<StoreMaterialEntity> edit(@RequestBody StoreMaterialEntity goodsMaterial) {
        goodsMaterialService.updateById(goodsMaterial);
        return Result.OK(goodsMaterialService.getById(goodsMaterial.getId()));
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品物料表-通过id删除")
    @ApiOperation(value = "商品物料表-通过id删除", notes = "商品物料表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        goodsMaterialService.removeById(id);
        return Result.OK("删除成功!");
    }

//    /**
//     * 批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "商品物料表-批量删除")
//    @ApiOperation(value = "商品物料表-批量删除", notes = "商品物料表-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
//    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
//        this.goodsMaterialService.removeByIds(Arrays.asList(ids.split(",")));
//        return Result.OK("批量删除成功!");
//    }

//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "商品物料表-通过id查询")
//    @ApiOperation(value = "商品物料表-通过id查询", notes = "商品物料表-通过id查询")
//    @GetMapping(value = "/queryById")
//    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
//        GoodsMaterialEntity goodsMaterial = goodsMaterialService.getById(id);
//        if (goodsMaterial == null) {
//            return Result.error("未找到对应数据");
//        }
//        return Result.OK(goodsMaterial);
//    }
//
//    /**
//     * 导出excel
//     *
//     * @param request
//     * @param goodsMaterial
//     */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, GoodsMaterialEntity goodsMaterial) {
//        return super.exportXls(request, goodsMaterial, GoodsMaterialEntity.class, "商品物料表");
//    }
//
//    /**
//     * 通过excel导入数据
//     *
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, GoodsMaterialEntity.class);
//    }

    /**
     * 商品物料表-使用
     *
     * @param id
     * @param goodsNum
     * @return
     */
    @AutoLog(value = "商品物料表-使用")
    @ApiOperation(value = "商品物料表-使用", notes = "商品物料表-使用")
    @PutMapping(value = "/goodMaterialUsed")
    public Result<?> goodsMaterialUsed(@RequestParam(name = "id") String id, @RequestParam(name = "goodsNum") int goodsNum) throws Exception {
        goodsMaterialService.goodsMaterialUsed(id, goodsNum);
        return Result.OK("编辑成功!");
    }

    /**
     * 同款补货
     *
     * @param beanReq
     * @return
     */
    @AutoLog(value = "商品物料表-同款补货")
    @ApiOperation(value = "商品物料表-同款补货", notes = "商品物料表-同款补货")
    @PostMapping(value = "/addStockNum")
    public Result<StoreMaterialRecordEntity> addStockNum(@RequestBody StoreMaterialEntity beanReq) throws Exception {
        return Result.OK(goodsMaterialService.addStockNum(beanReq));
    }

    /**
     * 添加
     *
     * @param goodsMaterial
     * @return
     */
    @AutoLog(value = "商品物料表-添加")
    @ApiOperation(value = "商品物料表-添加", notes = "商品物料表-添加")
    @PostMapping(value = "/add")
    public Result<StoreMaterialRecordEntity> add(@RequestBody StoreMaterialEntity goodsMaterial) throws Exception {
        StoreMaterialRecordEntity storeMaterialRecord = goodsMaterialService.add(goodsMaterial);
        return Result.OK(storeMaterialRecord);
    }

    /**
     * 添加-批量
     *
     * @param materialList
     * @return
     */
    @AutoLog(value = "商品物料表-批量添加")
    @ApiOperation(value = "商品物料表-批量添加", notes = "商品物料表-批量添加")
    @PostMapping(value = "/addBatch")
    public Result<ArrayList<StoreMaterialRecordEntity>> addBatch(@RequestBody ArrayList<StoreMaterialEntity> materialList) throws Exception {
        ArrayList<StoreMaterialRecordEntity> storeMaterialRecordList = goodsMaterialService.addBatch(materialList);
        return Result.OK(storeMaterialRecordList);
    }
}
